home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
10,000 Great Games
/
10,000 Great Games.iso
/
Product
/
66
/
data1.cab
/
Source_Files
/
Src
/
Data.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2000-01-16
|
2KB
|
114 lines
#include "stdafx.h"
cData *data = 0;
static char *database_fn;
cData::cData(char *_d, int _size, const char *_name)
{
d = _d;
size = _size;
name = strdup(_name);
add_end((cList **)&data);
}
cData::~cData()
{
free(name);
delete d;
}
void cData::add_to_file(char *fn, void *data, int size, const char *name)
{
// Open database file
CFile f;
if (!f.Open(fn, CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite | CFile::shareExclusive))
error("Unable to open %s", fn);
f.SeekToEnd();
// Write name of object + bytecount of string
int l = strlen(name);
f.Write(&l, sizeof(l));
f.Write(name, l);
// Write size
f.Write(&size, sizeof(size));
// Write data
f.Write(data, size);
}
static void create_add_file(const char *fn, const char *name)
{
// Read data
int size;
char *data = read_file(fn, &size);
// Put it in the database
cData::add_to_file(database_fn, data, size, name);
// Release temporary space
delete data;
}
void cData::create_file(char *fn, char *mask)
{
database_fn = fn;
search_files(".", mask, create_add_file);
}
void cData::load(char *fn)
{
char name[256], *data;
int l, size;
CFile f;
// Open database
if (!f.Open(fn, CFile::modeRead | CFile::shareDenyWrite))
error("Unable to open %s", fn);
while (f.Read(&l, sizeof(l)) == sizeof(l))
{
// Read name
f.Read(name, l);
name[l] = 0;
// Read size
f.Read(&size, sizeof(size));
// Read data
data = new char [size];
f.Read(data, size);
// Create data object
new cData(data, size, name);
}
}
void *cData::get(char *fn)
{
for (cData *d = data; d != 0; d = (cData *)d->next)
if (eq(fn, d->name))
return d->d;
error("Data object %s not found", fn);
return 0;
}